<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Javamail简单使用案例</h3>
            
            <small class="text-muted">发布于 2017-09-13</small>
        </div>

        <article>
            <h2 id="邮件开发环境搭建">邮件开发环境搭建</h2>
<h3 id="邮件服务器">邮件服务器</h3>
<ul>
<li>易邮邮件服务器</li>
<li>配置如下
<img src="https://ueyao.github.io/image-hosting/blog/2017/09/2017-09-13_085915.png" alt="易邮邮件服务器"></li>
</ul>
<p><img src="https://ueyao.github.io/image-hosting/blog/2017/09/2017-09-13_191012.png" alt="易邮邮件服务器配置"></p>
<h3 id="邮件客户端">邮件客户端</h3>
<ul>
<li>Foxmail</li>
<li>配置如下
<img src="https://ueyao.github.io/image-hosting/blog/2017/09/2017-09-13_192046.png" alt="Foxmail配置"></li>
</ul>
<h3 id="使用javamail发送邮件">使用Javamail发送邮件</h3>
<p>1、下载</p>
<ul>
<li>javamail-samples.zip</li>
<li>javax.mail.jar</li>
</ul>
<p>官网地址http://www.oracle.com/technetwork/java/javamail/index.html</p>
<p>2、使用javamail</p>
<p>发送到本地的邮件中</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#f92672">package</span> org.xueyao.email;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.Date;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.Properties;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Message;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Session;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Transport;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.internet.InternetAddress;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.internet.MimeMessage;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MailUtils</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * 发送邮件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param to   接收邮件的地址
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param subject  邮件主题
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param msgText   邮件内容
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">send</span>(String to, String subject, String msgText) {
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//发邮件的地址</span>
</span></span><span style="display:flex;"><span>        String from <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;admin@flowstone.com&#34;</span>; 
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//邮件发送服务器地址</span>
</span></span><span style="display:flex;"><span>        String host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;localhost&#34;</span>; 
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//是否开启debug模式</span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">boolean</span> debug <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>; 
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// 设置发送邮件的配置信息</span>
</span></span><span style="display:flex;"><span>        Properties props <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Properties();
</span></span><span style="display:flex;"><span>        props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.smtp.host&#34;</span>, host);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (debug) {
</span></span><span style="display:flex;"><span>            props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.debug&#34;</span>, debug);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//邮件会话</span>
</span></span><span style="display:flex;"><span>        Session session <span style="color:#f92672">=</span> Session.<span style="color:#a6e22e">getInstance</span>(props, <span style="color:#66d9ef">null</span>);
</span></span><span style="display:flex;"><span>        session.<span style="color:#a6e22e">setDebug</span>(debug);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">try</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//创建邮件</span>
</span></span><span style="display:flex;"><span>            MimeMessage msg <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> MimeMessage(session);
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setFrom</span>(<span style="color:#66d9ef">new</span> InternetAddress(from));
</span></span><span style="display:flex;"><span>            InternetAddress<span style="color:#f92672">[]</span> address <span style="color:#f92672">=</span> { <span style="color:#66d9ef">new</span> InternetAddress(to) };
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setRecipients</span>(Message.<span style="color:#a6e22e">RecipientType</span>.<span style="color:#a6e22e">TO</span>, address);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置主题</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setSubject</span>(subject);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置发送时间</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setSentDate</span>(<span style="color:#66d9ef">new</span> Date());
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">// If the desired charset is known, you can use</span>
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">// setText(text, charset)</span>
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置邮件的内容</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setText</span>(msgText);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//发送邮件</span>
</span></span><span style="display:flex;"><span>            Transport.<span style="color:#a6e22e">send</span>(msg);
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">catch</span> (Exception mex) {
</span></span><span style="display:flex;"><span>            mex.<span style="color:#a6e22e">printStackTrace</span>();
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>        String to <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;test02@flowstone.com&#34;</span>;
</span></span><span style="display:flex;"><span>        String subject <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;如何学习?&#34;</span>;
</span></span><span style="display:flex;"><span>        String msgText <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;解决学习困扰,就是天天晚上熬夜学习&#34;</span>;
</span></span><span style="display:flex;"><span>        MailUtils.<span style="color:#a6e22e">send</span>(to, subject, msgText);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>效果图</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2017/09/2017-09-13_193447.png" alt="效果图"></p>
<p>通过QQ邮箱发送</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#f92672">package</span> org.xueyao.email;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.Date;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> java.util.Properties;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Message;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Session;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.Transport;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.internet.InternetAddress;
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> javax.mail.internet.MimeMessage;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">MailQQUtils</span> {
</span></span><span style="display:flex;"><span>    <span style="color:#75715e">/**
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * 发送邮件
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param to   接收邮件的地址
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param subject  邮件主题
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     * @param msgText   邮件内容
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">     */</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">send</span>(String to, String subject, String msgText) {
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//发邮件的地址</span>
</span></span><span style="display:flex;"><span>        String from <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;931330220@qq.com&#34;</span>; 
</span></span><span style="display:flex;"><span>        String password <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;授权码&#34;</span>;
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//邮件发送服务器地址</span>
</span></span><span style="display:flex;"><span>        String host <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;smtp.qq.com&#34;</span>; 
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//是否开启debug模式</span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">boolean</span> debug <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>; 
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">// 设置发送邮件的配置信息</span>
</span></span><span style="display:flex;"><span>        Properties props <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> Properties();
</span></span><span style="display:flex;"><span>        props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.smtp.host&#34;</span>, host);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">if</span> (debug) {
</span></span><span style="display:flex;"><span>            props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.debug&#34;</span>, debug);
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//添加auth认证</span>
</span></span><span style="display:flex;"><span>        props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.smtp.auth&#34;</span>, <span style="color:#e6db74">&#34;true&#34;</span>);
</span></span><span style="display:flex;"><span>        props.<span style="color:#a6e22e">put</span>(<span style="color:#e6db74">&#34;mail.smtp.port&#34;</span>, <span style="color:#e6db74">&#34;587&#34;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>        <span style="color:#75715e">//邮件会话</span>
</span></span><span style="display:flex;"><span>        Session session <span style="color:#f92672">=</span> Session.<span style="color:#a6e22e">getInstance</span>(props, <span style="color:#66d9ef">null</span>);
</span></span><span style="display:flex;"><span>        session.<span style="color:#a6e22e">setDebug</span>(debug);
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">try</span> {
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//创建邮件</span>
</span></span><span style="display:flex;"><span>            MimeMessage msg <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> MimeMessage(session);
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setFrom</span>(<span style="color:#66d9ef">new</span> InternetAddress(from));
</span></span><span style="display:flex;"><span>            InternetAddress<span style="color:#f92672">[]</span> address <span style="color:#f92672">=</span> { <span style="color:#66d9ef">new</span> InternetAddress(to) };
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setRecipients</span>(Message.<span style="color:#a6e22e">RecipientType</span>.<span style="color:#a6e22e">TO</span>, address);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置主题</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setSubject</span>(subject);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置发送时间</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setSentDate</span>(<span style="color:#66d9ef">new</span> Date());
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//设置邮件的内容</span>
</span></span><span style="display:flex;"><span>            msg.<span style="color:#a6e22e">setText</span>(msgText);
</span></span><span style="display:flex;"><span>            <span style="color:#75715e">//发送邮件</span>
</span></span><span style="display:flex;"><span>            Transport.<span style="color:#a6e22e">send</span>(msg,from,password);
</span></span><span style="display:flex;"><span>        } <span style="color:#66d9ef">catch</span> (Exception mex) {
</span></span><span style="display:flex;"><span>            mex.<span style="color:#a6e22e">printStackTrace</span>();
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span>(String<span style="color:#f92672">[]</span> args) {
</span></span><span style="display:flex;"><span>        String to <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;收件邮箱&#34;</span>;
</span></span><span style="display:flex;"><span>        String subject <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;如何学习?&#34;</span>;
</span></span><span style="display:flex;"><span>        String msgText <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;解决学习困扰,就是天天晚上熬夜学习&#34;</span>;
</span></span><span style="display:flex;"><span>        MailQQUtils.<span style="color:#a6e22e">send</span>(to, subject, msgText);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span></code></pre></div><p>效果图</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2017/09/2017-09-13_211125.png" alt=""></p>

        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
